#!/usr/bin/python3.4
# -*- coding=utf-8 -*-

from ldap3 import Server, Connection, AUTO_BIND_NO_TLS, LEVEL, SUBTREE, BASE, ALL_ATTRIBUTES, ObjectDef, AttrDef, Reader, Entry, Attribute,OperationalAttribute 

import ldap3

def findCNName(username):
    server = Server('qytang.com')
	c = Connection(server, auto_bind=AUTO_BIND_NO_TLS, user='qytang\xxxx', password='xxxx')
	c.search(search_base = 'ou=CCIESECURITY, ou=Security, ou=QYT, dc=qytang, dc=com',
         	 search_filter='(sAMAccountName=%s)' % username,
         	 search_scope=LEVEL,
         	 attributes = ALL_ATTRIBUTES,
         	 paged_size = 100)
	return c.response[0]['attributes']['displayName'][0]

if __name__ == '__main__':
	print(findCNName('sec-zzy'))


